﻿var Bag = function(opt) {
    this.size = opt.size || 40 //背包大小
    this.empty = opt.empty || 40 //背包剩余大小
    this.items = opt.items || [] //背包物品
}

module.exports = Bag

Bag.prototype.addItems = function(items) {
    var fullflag = false //背包是否满了 0还有空间，1满了
    var bag = this
    var getItems = []
    for (var i in items) {
        var itemid = items[i]
        var maxNum = 99
        //查找是否还有相同的物品
        var index = bagIndex(bag.items, itemid)
        if (index[0] === -1) { //[-1,i]
            //没有相同的物品，判断背包是否还有空间
            if (bag.empty > 0) {
                //还有空间
                bag.items.splice(index[1], 0, [itemid, 1])
                bag.empty -= 1
                getItems.push(itemid)
            } else {
                //没空间了，添加失败
                fullflag = true
            }
        } else {
            //有相同的物品，
            //判断是否可以叠加
            var m = bag.items[index[0]]

            //判断是否还可以叠加
            if (m[1] % maxNum !== 0) {
                m[1] += 1
            } else {
                //判断是否还有空间
                if (bag.empty > 0) {
                    m[1] += 1
                    bag.empty -= 1
                } else {
                    //没空间了，添加失败
                    fullflag = true
                }
            }
        }
    }
    return getItems
}

Bag.prototype.addItem = function(itemid) {
    var getItem = 0
    //查找是否还有相同的物品
    var index = bagIndex(this.items, itemid)
    if (Array.isArray(index)) { //[-1,i]
        //没有相同的物品，判断背包是否还有空间
        if (this.empty > 0) {
            //还有空间
            this.items.splice(index[1], 0, [itemid, 1])
            this.empty -= 1
            getItems = itemid
        }
    }
    return getItem
}

//获取物品的数量，物品不存在则返回0
Bag.prototype.getItemCount = function(itemid) {
    var index = bagIndex(this.items, itemid)
    if (index[0] === -1) {
        return 0
    } else {
        return this.items[index[0]][1]
    }
}

Bag.prototype.reduceItem = function(itemid) {
    if (itemid < 1000000) {
        var number
        if (arguments.length > 1) {
            number = arguments[1]
        } else {
            number = 1
        }
        var index = bagIndex(this.items, itemid)
        if (index[0] === -1) {
            return -1
        }
        var m = this.items[index[0]][1]
        if (m < number) {
            return -2
        }
        var maxNum = 99
        if (m === number) {
            this.empty += Math.ceil(m / maxNum)
            this.items.splice(index, 1)
            return 0
        }
        this.empty += Math.ceil(m / maxNum) - Math.ceil((m - number) / maxNum)
        this.items[index][1] -= number
        return 0
    } else {
        var player

        var index = bagIndex(this.items, itemid)
        if (index[0] === -1) {
            return -4
        }
        var m = this.items[index[0]][1]
        this.empty += 1
        this.items.splice(index[0], 1)
        if (arguments.length > 1 && typeof arguments[1] === 'object') {
            player = arguments[1]
            delete player.itemInfo['' + itemid]
        }
        return 0
    }
}


function bagIndex(list, itemid) {
    if (list.length === 0) {
        return [-1, 0]
    }
    var low = 0
    var high = list.length - 1
    var mid = parseInt((low + high) / 2)
    var index
    while (list[mid][0] !== itemid && low < high) {
        if (list[mid][0] > itemid) {
            high = mid - 1
        } else if (list[mid][0] < itemid) {
            low = mid + 1
        }
        //重新计算中项索引 
        mid = parseInt((low + high) / 2)
    } // 循环结束
    // 返回index
    if (list[mid][0] !== itemid) {
        if (list[mid][0] < itemid) {
            index = [-1, mid + 1]
        } else {
            index = [-1, mid]
        }
    } else {
        index = [mid]
    }
    return index
}